use Getopt::Long;

use strict;

my $opt_file;
my $opt_start;
my $opt_end;
my $opt_user;

my $pid = "$$";

sub process_options
{

    if( !GetOptions( "file=s" => \$opt_file,
                     "start=s" => \$opt_start,
                     "end=s" => \$opt_end,
                     "user=s" => \$opt_user ) )
    {
        print "Illegal options, aborted.\n";
        exit 1;
    }

    if( !(defined $opt_start) )
    {
        $opt_start = "00000000000000";
    }
    
    if( !(defined $opt_end) )
    {
        $opt_end = "99999999999999";
    }
    
}

sub main()
{
    &process_options();

    my @start_arr = ( $opt_start =~ m/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?(\d\d)?$/ );
    my @end_arr = ( $opt_end =~ m/^(\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?(\d\d)?$/ );

    if( $#start_arr != 5 && $#start_arr != 3 )
    {
        print "Illegal syntax for optin 'start'\n";
        exit 1;
    }
    if( $#end_arr != 5 && $#end_arr != 3 )
    {
        print "Illegal syntax for optin 'end'\n";
        exit 1;
    }
    if( $#start_arr == 5 )
    {
        my $i = 3;
        while( $i <= 5 )
        {
            if( $start_arr[ $i ] eq "" )
            {
                $start_arr[ $i ] = "00";
            }
            ++$i;
        }
    }
    if( $#end_arr == 5 )
    {
        my $i = 3;
        while( $i <= 5 )
        {
            if( $end_arr[ $i ] eq "" )
            {
                $end_arr[ $i ] = "99";
            }
            ++$i;
        }
    }

    my $plotdata_file = ".plotdata.tmp.$pid";
    open OH, "> $plotdata_file" or die "Cannot write to \"$plotdata_file\": $!\n";
    my $ohline = 0;

    open FH, "< $opt_file" or die "Failed to open \"$opt_file\": $!\n";
    my $line;
    while( $line = <FH> )
    {
        chomp $line;
        if( $line =~ m!^(\d{14}) ([a-zA-Z0-9]+)\[(\d+)\] (\d+)k\((\d+)M\) (\d+)k\((\d+)M\) (\d+)k\((\d+)M\) (\d+.\d\d)%$! )
        {
            my $a = $1;
            my $time = $a;
            my $the_user = $2;
            my $mem_virtual = $5;
            my $mem_res = $7;
            my $mem_shr = $9;
            my $cpu_percentage = $10;
            my @a = ($a=~m/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/);
            if( $#a != 5 )
            {
                die "Internal error, please investigate.\n";
            }
            my $skip = 0;
            my $i = 0;
            while( $i <= $#start_arr && $skip == 0 )
            {
                if( $a[$i] < $start_arr[$i] )
                {
                    $skip = 1;
                }
                if( $a[$i] > $start_arr[$i] )
                {
                    last;
                }
                ++$i;
            }
            $i = 0;
            while( $i <= $#end_arr && $skip == 0 )
            {
                if( $a[$i] > $end_arr[$i] )
                {
                    $skip = 1;
                }
                if( $a[$i] < $end_arr[$i] )
                {
                    last;
                }
                ++$i;
            }
            if( $skip == 0 )
            {
                if( ( ( defined $opt_user ) && $opt_user eq $the_user ) ||
                    !(defined $opt_user ) )
                {
                    my $mem_report = $mem_res - $mem_shr;
                    ++$ohline;
                    print OH "$time $mem_report $cpu_percentage\n";
                }
            }
        }
    }

    close FH;
    close OH;

    if( !( defined $opt_user ) )
    {
        $opt_user = "ALL";
    }

    if( $ohline > 0 )
    {
        my $plotcmd_file = ".plotcmd.tmp.$pid";

        my $cpu_png = "cpu__${opt_user}.png";

        system "sed -e s/%%PLOTDATA_FILE%%/$plotdata_file/g -e s/%%USER%%/$opt_user/g -e s/%%OUTPUT_FILE%%/$cpu_png/g plot.template.cpu_usage > $plotcmd_file"; # or die "Failed to sed plot.template.cpu_usage, please investigate.\n";
        system "gnuplot $plotcmd_file"; # or die "Failed to plot, please invesitgate.\n";

        my $mem_png = "mem__${opt_user}.png";

        system "sed -e s/%%PLOTDATA_FILE%%/$plotdata_file/g -e s/%%USER%%/$opt_user/g -e s/%%OUTPUT_FILE%%/$mem_png/g plot.template.mem_usage > $plotcmd_file"; # or die "Failed to sed plot.template.cpu_usage, please investigate.\n";
        system "gnuplot $plotcmd_file"; # or die "Failed to plot, please invesitgate.\n";

        unlink $plotdata_file;
        unlink $plotcmd_file;

        printf "Generated: $cpu_png $mem_png\n";

    }
    else
    {
        print "No matching data found.\n";
    }
}

&main;

